package com.lelouch.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * CountDownLatch
 *
 * @author lcy
 * @create 2018-05-29 10:09
 */
@Slf4j
public class CountDownLatchExample2 {


    private final static int threadCount = 200;

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount; i++) {
            final int number =i;
            executorService.execute(()->{
                try {
                    test(number);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });

        }
        try {
            countDownLatch.await(10, TimeUnit.MILLISECONDS);
            log.info("finish");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();

    }

    public static void  test(int threadNum) throws InterruptedException {
        Thread.sleep(1);
        log.info("count:{}",threadNum);
    }


}
